
const VUE_APP_BUCKET_NAME='tempfile-1253210252'
const VUE_APP_REGION='ap-guangzhou'
const VUE_APP_IMGURL='https://tempfile-1253210252.cos.accelerate.myqcloud.com/'

import COS from 'cos-js-sdk-v5';
import { getKey } from './http';
export function getRandom (a, b) {
  let max = a;
  let min = b;
  if (a < b) {
    max = b;
    min = a;
  }
  return parseInt((Math.random() * (max - min))) + min;
}
export function suijione (num, maxA, minlA, fqy) {
  const arr = [];
  const arr1 = [];
  const arr2 = [];
  if (num) {
    for (let m = 0; m <= 9; m++) {
      arr.push(m);
    }
  }
  if (maxA) {
    for (let m = 65; m <= 90; m++) {
      arr1.push(m);
    }
  }
  if (minlA) {
    for (let m = 97; m <= 122; m++) {
      arr2.push(m);
    }
  }
  if (!fqy) {
    console.log('生成位数必传');
  }
  const mergeArr = arr.concat(arr1);
  const mergeArr1 = mergeArr.concat(arr2);
  const _length = mergeArr1.length;
  let text = '';
  for (let m = 0; m < fqy; m++) {
    let text1 = '';
    const random = getRandom(0, _length);
    if (mergeArr1[random] <= 9) {
      text1 = mergeArr1[random];
    } else if (mergeArr1[random] > 9) {
      text1 = String.fromCharCode(mergeArr1[random]);
    }
    text += text1;
  }
  return text;
}


/**
 * @description: 上传图片
 * @param {*} file 上传的文件和存储桶的名字
 * @return {*} 上传后的图片url
 * @author: lihk
 */
export async function initCOS (file, bucket_name, isMd5 = 'flase', fileName, isUploadCOS = false) {
  let folder = VUE_APP_IMGURL.split('/')[3];
  folder = fileName ? folder + fileName : folder;
  const name = file.name.split('.')[1];
  console.log("🚀 ~ file: upload2.js:70 ~ initCOS ~ name:", name)
  const random = suijione(true, true, false, 6);
  console.log("🚀 ~ file: upload2.js:70 ~ initCOS ~ random:", random)
  const uploadFileName = Date.parse(new Date());
  const oldFileName = file.name.split('.')[0];
  console.log('folder', VUE_APP_BUCKET_NAME);

  const cos = new COS({
    getAuthorization: async function (options, callback) {
      const { data } = await getKey(VUE_APP_BUCKET_NAME, VUE_APP_REGION);
      callback({
        TmpSecretId: data.credentials.tmpSecretId,
        TmpSecretKey: data.credentials.tmpSecretKey,
        SecurityToken: data.credentials.sessionToken,
        StartTime: data.startTime,
        ExpiredTime: data.expiredTime,
        ScopeLimit: true
      });
    }
  });

  try {
    const key = isMd5 ? `${folder}/${oldFileName}_${uploadFileName}${random}.${name}` : `${folder}/${uploadFileName}${random}.${name}`;
    const res = await cos.putObject({
      Bucket: VUE_APP_BUCKET_NAME,
      Region: VUE_APP_REGION,
      Key: key,
      StorageClass: 'STANDARD',
      Body: file
    });

    console.log(res);
    if (res.statusCode == 200) {
    }

    console.log(
      '打印上传图片的名称',
      res.Location.split('oem-1253210252.cos.eu-frankfurt.myqcloud.com/')[1]
    );

    const result = {
      md5: JSON.parse(res.headers?.etag),
      url: 'https://' + res.Location
    };

    console.log('isMd5', isMd5);
    if (isUploadCOS) return 'https://' + res.Location.replace('eu-frankfurt', 'accelerate');
    return isMd5 ? result : res.Location.split('oem-1253210252.cos.eu-frankfurt.myqcloud.com/')[1];
  } catch (error) {
    console.log(error);
  }
}